<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Exception;
use think\exception\DbException;
use think\response\Json;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Order;
    }


    /**
     * 列表
     * @return string|Json
     * @throws Exception
     * @throws DbException
     */
    public function index()
    {
        // 设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        // 请求参数
        $param = $this->request->param();

        // 提交查询
        if ($this->request->isAjax()) {
            // 如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            [$where, $sort, $order, $offset, $limit] = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $list = $list ? $list->toArray() : [];
            $rows = $list['data'];

            // 填充数据
            $dict = $this->model->dict;
            foreach ($rows as $key => $item) {
                $rows[$key]['status_name'] = $dict['status'][$item['status']] ?? '';
                $rows[$key]['order_type_name'] = $dict['order_type'][$item['order_type']] ?? '';
                //$rows[$key]['is_agent_name'] = $dict['is_agent'][$item['is_agent']] ?? '';
            }

            $result = array("total" => $list['total'], "rows" => $rows);
            return json($result);
        }

        return $this->view->fetch();
    }

}
